package com.example.shell.client;

import cn.hutool.http.HttpUtil;
import com.example.shell.model.entity.AttendanceRecord;
import com.example.shell.model.entity.CheckinListRequest;
import com.example.shell.model.entity.UserInfo;
import com.example.shell.util.ParseDomXPath;
import com.example.shell.util.XOREncryption;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class OaClient {

    private final String BASE_URL = "http://192.168.0.83:8801/ELWCF.svc";

    public UserInfo login(String sEmployeeCode, String password){
        String xorPassword = XOREncryption.encrypt(password,"ENLONG");

        String body = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "    <s:Header>\n" +
                "        <ELUSERIDENTITY xmlns=\"http://www.EnLong.cn\"></ELUSERIDENTITY>\n" +
                "        <ELCURRENTCLIENTVERSION xmlns=\"http://www.EnLong.cn\"></ELCURRENTCLIENTVERSION>\n" +
                "    </s:Header>\n" +
                "    <s:Body>\n" +
                "        <Invoke xmlns=\"EnLong.Lib.WCF\">\n" +
                "            <info xmlns:d4p1=\"http://schemas.datacontract.org/2004/07/EnLong.Lib.WCF\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
                "                <d4p1:Name>UserLogin.LoginOfSkipCheckProductLicense</d4p1:Name>\n" +
                "                <d4p1:Parameters xmlns:d5p1=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\n" +
                "                    <d5p1:anyType xmlns:d6p1=\"http://schemas.datacontract.org/2004/07/EnLong.BPSupports.SysStandardDefine\" i:type=\"d6p1:DTOUserBaseInfo\">\n" +
                "                        <d6p1:ClientCheckCode></d6p1:ClientCheckCode>\n" +
                "                        <d6p1:ClientCheckCodeIdentity></d6p1:ClientCheckCodeIdentity>\n" +
                "                        <d6p1:ClientIPAddress></d6p1:ClientIPAddress>\n" +
                "                        <d6p1:ClientMACAddress></d6p1:ClientMACAddress>\n" +
                "                        <d6p1:ClientUSBKey></d6p1:ClientUSBKey>\n" +
                "                        <d6p1:IndependentCode></d6p1:IndependentCode>\n" +
                "                        <d6p1:IndependentName></d6p1:IndependentName>\n" +
                "                        <d6p1:IsDisabled>false</d6p1:IsDisabled>\n" +
                "                        <d6p1:IsFirstLoginModifyPwd>false</d6p1:IsFirstLoginModifyPwd>\n" +
                "                        <d6p1:JobCode></d6p1:JobCode>\n" +
                "                        <d6p1:JobName></d6p1:JobName>\n" +
                "                        <d6p1:LoginID>"+sEmployeeCode+"</d6p1:LoginID>\n" +
                "                        <d6p1:LoginIdentity></d6p1:LoginIdentity>\n" +
                "                        <d6p1:LoginSubSystemCode>20</d6p1:LoginSubSystemCode>\n" +
                "                        <d6p1:LoginSubSystemName></d6p1:LoginSubSystemName>\n" +
                "                        <d6p1:LoginType>0</d6p1:LoginType>\n" +
                "                        <d6p1:LoginVerificationSubSystemCode>20</d6p1:LoginVerificationSubSystemCode>\n" +
                "                        <d6p1:OrgCode></d6p1:OrgCode>\n" +
                "                        <d6p1:OrgName></d6p1:OrgName>\n" +
                "                        <d6p1:Password>"+xorPassword+"</d6p1:Password>\n" +
                "                        <d6p1:SF1></d6p1:SF1>\n" +
                "                        <d6p1:SF2></d6p1:SF2>\n" +
                "                        <d6p1:SF3></d6p1:SF3>\n" +
                "                        <d6p1:SF4></d6p1:SF4>\n" +
                "                        <d6p1:SF5></d6p1:SF5>\n" +
                "                        <d6p1:UserCode></d6p1:UserCode>\n" +
                "                        <d6p1:UserName></d6p1:UserName>\n" +
                "                        <d6p1:UserType>1</d6p1:UserType>\n" +
                "                    </d5p1:anyType>\n" +
                "                </d4p1:Parameters>\n" +
                "                <d4p1:Parent i:nil=\"true\" />\n" +
                "            </info>\n" +
                "        </Invoke>\n" +
                "    </s:Body>\n" +
                "</s:Envelope>";


        String resp = HttpUtil.createPost(BASE_URL)
                .header("Soapaction", "\"EnLong.Lib.WCF/IInvokeService/Invoke\"")
                .header("Content-Type", "text/xml; charset=utf-8")
                .body(body)
                .timeout(10000)
                .execute().body();


        try {
            return ParseDomXPath.parseLoginResult(resp);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public List<AttendanceRecord> checkinList(CheckinListRequest request){
        String body = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "    <s:Header>\n" +
                "        <ELUSERIDENTITY xmlns=\"http://www.EnLong.cn\">"+request.getToken()+"</ELUSERIDENTITY>\n" +
                "        <ELCURRENTCLIENTVERSION xmlns=\"http://www.EnLong.cn\"></ELCURRENTCLIENTVERSION>\n" +
                "    </s:Header>\n" +
                "    <s:Body>\n" +
                "        <Invoke xmlns=\"EnLong.Lib.WCF\">\n" +
                "            <info xmlns:d4p1=\"http://schemas.datacontract.org/2004/07/EnLong.Lib.WCF\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
                "                <d4p1:Name>GridService.GetDataCollectionBySQL</d4p1:Name>\n" +
                "                <d4p1:Parameters xmlns:d5p1=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\n" +
                "                    <d5p1:anyType xmlns:d6p1=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d6p1:string\">select  *  from (select sEmployeeCode,dDate,F1,F2,F3,F4,F5,F6,CONVERT(int,ROW_NUMBER() OVER( order by  sEmployeeCode ) ) as EnLongIndex " +
                "from HR_Attendance_TimesRecord " +
                "where ( (  ( dDate BETWEEN '"+request.getStartDate()+"' and '"+request.getEndDate()+" ') " +
                "and  sEmployeeCode = '51083'  )  and  (  (  ( 1=1 ) AND ( 1=1 )  )  ) )) as temp&#xD;\n" +
                "\t\t\t\t where EnLongIndex between 1 and "+request.getLimit()+"   order by  sEmployeeCode &#xD;\n" +
                "select  count(0) from HR_Attendance_TimesRecord where ( (  ( dDate BETWEEN '"+request.getStartDate()+"' and '"+request.getEndDate()+" ') " +
                "and  sEmployeeCode = '"+request.getSEmployeeCode()+"'  )  and  (  (  ( 1=1 ) AND ( 1=1 )  )  ) )&#xD;\n" +
                "</d5p1:anyType>\n" +
                "                    <d5p1:anyType xmlns:d6p1=\"http://www.w3.org/2001/XMLSchema\" i:type=\"d6p1:string\">HR01</d5p1:anyType>\n" +
                "                </d4p1:Parameters>\n" +
                "                <d4p1:Parent i:nil=\"true\" />\n" +
                "            </info>\n" +
                "        </Invoke>\n" +
                "    </s:Body>\n" +
                "</s:Envelope>";


        String resp = HttpUtil.createPost(BASE_URL)
                .header("Soapaction", "\"EnLong.Lib.WCF/IInvokeService/Invoke\"")
                .header("Content-Type", "text/xml; charset=utf-8")
                .body(body)
                .timeout(10000)
                .execute().body();


        try {
            return ParseDomXPath.parseCheckinListResult(resp);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
